package com.example.fresh_market.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.request.RequestOptions;
import com.example.fresh_market.R;
import com.example.fresh_market.entity.Cart;
import com.example.fresh_market.request.PreOrderDetail;
import com.example.fresh_market.utils.HttpUtil;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class CartAdapter extends RecyclerView.Adapter<CartAdapter.OrderViewHolder> {
    private final ArrayList<Cart> mList;
    private final CartAdapter.MyClickListener mListener;

    public CartAdapter(ArrayList<Cart> list, CartAdapter.MyClickListener listener) {
        this.mList = list;
        this.mListener = listener;
    }


    @NonNull
    @Override
    public OrderViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new OrderViewHolder(LayoutInflater.from(viewGroup.getContext())
                .inflate(R.layout.cart_list_item, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull OrderViewHolder holder, int position) {
        Cart target = getItem(position);
        holder.bindBean(target);
        holder.BT_add.setOnClickListener(v -> {
            mListener.onAddClick(v, holder.getAdapterPosition());
        });
        holder.BT_subtract.setOnClickListener(v -> {
            mListener.onSubClick(v, holder.getAdapterPosition());
        });
        holder.cart_checkBox.setOnClickListener(v -> {
            mListener.onCheckBoxClick(v, holder.getAdapterPosition());
        });
        holder.number.setOnClickListener((v) -> {
            mListener.onCartNumClick(v, holder.getAdapterPosition());
        });
    }

    public Cart getItem(int position) {
        return mList.get(position);
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public void setData(List<Cart> list) {
        mList.clear();
        mList.addAll(list);
        notifyDataSetChanged();
    }

    public List<Cart> getAllItems() {
        return mList;
    }

    public List<PreOrderDetail> getPreOrderDetails() {
        List<PreOrderDetail> list = new ArrayList<>();
        for (Cart cart : mList) {
            if (cart.isChecked()) {
                PreOrderDetail detail = new PreOrderDetail();
                detail.setShoppingCartId(cart.getId());
                list.add(detail);
            }
        }
        return list;
    }

    public void updateItem(int position, Cart cart) {
        mList.set(position, cart);
        notifyItemChanged(position);
    }

    public void removeData(int index) {
        if (index == -1) {
            int size = mList.size();
            mList.clear();
            notifyItemRangeRemoved(0, size);
        } else {
            mList.remove(index);
            notifyItemRemoved(index);
        }
    }

    public interface MyClickListener {

        void onAddClick(View v, int position);

        void onSubClick(View v, int position);

        void onCheckBoxClick(View v, int position);

        void onCartNumClick(View v, int position);
    }

    public static class OrderViewHolder extends RecyclerView.ViewHolder {
        private final CheckBox cart_checkBox;
        private final TextView name;
        private final TextView info;
        private final TextView price;
        private final ImageView image;
        private final TextView number;
        private final ImageButton BT_add;
        private final ImageButton BT_subtract;

        OrderViewHolder(View itemView) {
            super(itemView);
            cart_checkBox = itemView.findViewById(R.id.cart_checkBox);
            name = itemView.findViewById(R.id.cart_productName);
            info = itemView.findViewById(R.id.cart_productInfo);
            price = itemView.findViewById(R.id.cart_productPrice);
            image = itemView.findViewById(R.id.img_drink);
            number = itemView.findViewById(R.id.cart_productNum);
            BT_add = itemView.findViewById(R.id.button_add);
            BT_subtract = itemView.findViewById(R.id.button_subtract);
        }

        void bindBean(final Cart cart) {
            name.setText(cart.getProductName());
            info.setText(cart.getProductInfo());
            number.setText(String.valueOf(cart.getProductNum()));
            price.setText(String.format("¥%s", cart.getProductPrice()));
            cart_checkBox.setChecked(cart.isChecked());
            if (StringUtils.isNotBlank(cart.getProductImg())) {
                HttpUtil.loadImage(image, cart.getProductImg(), new RequestOptions().override(400, 400));
            }
        }
    }
}
